// pages/mall/mall.js
const app = getApp()
var help = require('../../utils/help.js');
var login = require('../../utils/login.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //轮播图
    imgarr: [],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,

    //模态框
    showOneButtonDialog: false,
    showTwoButtonDialog: false,

    data: [],
    page: 1, // 当前页
    is_load: true, // 是否继续加载
    load_lock: false, // 加载锁
    load_ts: '',
    keyword: '',

    showGoodsData: {},
    num: 1,
    config: {},

    cate_list:[],
    cate_id:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  //模态框代码
  tapDialogButton(e) {
    this.setData({
      num: 1,
      showOneButtonDialog: false,
      showTwoButtonDialog: false
    })
  },
  tapOneDialogButton(e) {
    var that = this;
    var i = 0;
    var showGoodsData = {};
    for (i in that.data.data) {
      console.log(that.data.data[i])
      if (that.data.data[i].id == e.currentTarget.dataset.id) {
        showGoodsData = that.data.data[i];
        break;
      }
    }
    this.setData({
      showGoodsData: showGoodsData,
      showOneButtonDialog: true
    })
  },
  jian: function() {
    if (this.data.num > 1) {
      this.setData({
        num: this.data.num - 1
      })
    }
  },
  jia: function () {
    if (this.data.num < this.data.showGoodsData.kucun) {
      this.setData({
        num: this.data.num + 1
      })
    }
  },
  set_num: function(e)
  {
    var num = e.detail.value;
    if (num <= 1) {
      num = 1;
    } else if (num >= this.data.showGoodsData.kucun) {
      num = this.data.showGoodsData.kucun;
    }
    this.setData({
      num: num
    })
  },
  tapTwoDialogButton(e) {
    var that = this;
    //幻灯片
    help.get(app.data.apiUrl + 'Goods/addCard', {
      token: app.data.userInfo ? app.data.userInfo.token : '',
      goods_id: that.data.showGoodsData.id,
      num: that.data.num
    }, that, function (res, that) {
      var result = res.data;
      // console.log(result);
      if (result.code == 1) {
        that.setData({
          num:1,
          showOneButtonDialog: false,
          showTwoButtonDialog: true
        })
      } else {
        help.msg(result.msg)
      }

    });

    
  },
  closePopup:function(e){
    this.setData({
      showTwoButtonDialog: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.getConfig(this);
    this.loadData(true);
    this.loadBaseData();
  },
  set_keyword: function (e) {
    this.setData({
      keyword: e.detail.value
    })
    this.loadData(true);
  },

  /**
   * 加载数据
   */
  loadData: function (is_one) {
    var that = this;

    if (is_one === true) {
      that.setData({
        is_load: true
      })
    }
    if (this.data.load_lock || !this.data.is_load) {
      return false;
    }
    this.setData({
      load_lock: true,
      load_ts: '正在加载'
    })
    if (is_one === true) {
      that.setData({
        page: 1,
        data: []
      })
    }

    var url = app.data.apiUrl + 'Goods/index';
    var data = {
      token: app.data.userInfo ? app.data.userInfo.token : '',
      keyword: that.data.keyword,
      page: that.data.page,
      cate_id: that.data.cate_id
    };
    help.get(url, data, that, function (res, that) {
      that.setData({
        load_lock: false
      })
      var result = res.data;
      console.log(result);
      if (result.code == 1) {
        if (result.data.length > 0) {
          if (is_one === true) {
            var new_data = result.data;
          } else {
            var new_data = that.data.data.concat(result.data)
          }
          console.log(new_data)
          that.setData({
            data: new_data,
            load_ts: ''
          })
        } else {
          if (is_one === true) {
            that.setData({
              data: [],
            })
          }
          that.setData({
            is_load: false,
            load_ts: '没有更多数据了',

          })
        }
        that.data.page++;

      } else if (result.code == 401) {
        help.msg('登录已失效，正在重新登录...')
        // 重新检测登录状态
        login.getUserInfo(app);
        return false;
      } else {
        help.msg(result.msg)
        return false;
      }

    });
  },

  loadBaseData: function () {
    var that = this;
    //幻灯片
    help.get(app.data.apiUrl + 'Index/getBanner', {}, that, function (res, that) {
      var result = res.data;
      // console.log(result);
      if (result.code == 1) {
        that.setData({
          imgarr: result.data,
        })
      }

    });

    help.get(app.data.apiUrl + 'Goods/getCateAll', {
      token: app.data.userInfo ? app.data.userInfo.token : '',
    }, that, function (res, that) {
      var result = res.data;
      // console.log(result);
      if (result.code == 1) {
        that.setData({
          cate_list: result.data,
        })
      }

    });

  },

  setcate:function(e) {
    console.log(e)
    this.setData({
      cate_id: e.currentTarget.dataset.id
    })
    this.loadData(true);
  },

  // 文章
  onclicktab: function(e) {
    help.setCache('show_page_cate_id', e.currentTarget.dataset.id);
    help.goUrlTab('/pages/index/index');
  },

  // 专家
  zhuanjia: function(){
    help.goUrl('/pages/consultation/experConsult/experConsult');
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    app.getConfig(this);
    this.loadData(true);
    this.loadBaseData();
    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  goCouponType: function () {
    help.goUrl('/pages/coupon_type/coupon_type')
  }
  
})